<?php


namespace app\http\middleware;
use Response;


class Limit
{
    public static $num = 1;
    public static $time = 10;

    public function handle($request, \Closure $next)
    {
        /**
         * api接口限流
         */
        $ip = request()->ip();
        $redis_key = md5($ip);
        $num = redis()->get($redis_key) ?: 0;
        if(!redis()->exists($redis_key)){
            redis()->set($redis_key,1,self::$time);
        }else{
            if($num >= self::$num){
                return jsonp_error(403,'亲，强撸灰飞烟灭！');
            }
        }
        return $next($request);
    }
}